function log(msg) {
    if (window.console) {
        console.log(msg);
    }
    var log = document.getElementsByTagName('textarea')[0];
    log.innerHTML = msg + '\r' + log.innerHTML;
}

// helper to toggle CSS class
function toggleClass(elem, name) {
    if (elem.className.indexOf(name) < 0) {
        elem.className += ' ' + name;
    } else {
        elem.className = elem.className.split(name).join('');
    }
}

//function clearSelection() {
//var selection =
//window.getSelection ? window.getSelection() :
//document.getSelection ? document.getSelection() :
//document.selection;
//if (selection && selection.removeAllRanges) {
// W3C-style
//	selection.removeAllRanges();
//} else if (selection && selection.empty) {
// IE-style
//	selection.empty();
//}
//}
// generic handler for either event
function printEvt(e) {
    log(this.id + ', ' + e.type + ': ' + e.detail);
    toggleClass(this, 'on' + e.type);
    //	clearSelection();
}

var HAS_TOUCH = ('ontouchstart' in window);

// ----------------------------------------------------
// Do not enable touch clicks, but monitor both.
// NOTE: native clicks will fire but touch will not.
var none = document.getElementById('none');
this.addEventListener('click', printEvt, false);
this.addEventListener('dblclick', printEvt, false);

// ----------------------------------------------------
// Enable click & dblclick events, and monitor both.
var both = document.getElementById('both');
this.addEventListener(HAS_TOUCH ? 'touchend' : 'mouseup', false);

//doubleTap(), false);
this.addEventListener('tap', printEvt, false);
this.addEventListener('dbltap', printEvt, false);
